package com.example.coffeeseller_2.model.helper.view_helper;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.AppCompatImageView;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.coffeeseller_2.model.beans.PointAttr;
import com.example.coffeeseller_2.utils.DensityUtil;

/**
 * Created by WH on 2018/7/23.
 * 创建滑动小圆点布局
 */

public class CreatePointHelper {

    private static String TAG = "CreatePointHelper";
    private Context context;
    private PointAttr pointAttr;
    private int curSelectedIndex;
    private GradientDrawable pointSelectedDrawable;
    private GradientDrawable pointUnSelectedDrawable;
    private LinearLayout pointLayout;

    public CreatePointHelper(Context context) {

        this.context = context;
        createPointLayout();
    }

    /*
    * 根据圆点属性创建圆点
    * */
    public void create(PointAttr pointAttr) {

        this.pointAttr = pointAttr;
        this.pointSelectedDrawable = generateDefaultDrawable(pointAttr.getPointSelectedColor());
        this.pointUnSelectedDrawable = generateDefaultDrawable(pointAttr.getPointUnSelectedColor());
        createIndicators();
    }


    private void createPointLayout() {
        pointLayout = new LinearLayout(context);
        pointLayout.setOrientation(LinearLayout.HORIZONTAL);
        pointLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER);
    }

    /**
     * 绘制小圆点
     */
    private GradientDrawable generateDefaultDrawable(int color) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setSize(pointAttr.getPointSize(), pointAttr.getPointSize());
        gradientDrawable.setCornerRadius(pointAttr.getPointSize());
        gradientDrawable.setColor(color);
        return gradientDrawable;
    }

    /**
     * 创建小圆点
     */
    private void createIndicators() {
        pointLayout.removeAllViews();
        for (int i = 0; i < pointAttr.getPointNum(); i++) {
            AppCompatImageView img = new AppCompatImageView(context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = pointAttr.getPointMargin() / 2;
            lp.rightMargin = pointAttr.getPointMargin() / 2;
            if (pointAttr.getPointMargin() >= DensityUtil.dp2px(context, 4)) { // 设置了indicatorSize属性
                lp.width = lp.height = DensityUtil.dp2px(context, pointAttr.getPointSize());
            } else {
                // 如果设置的resource.xml没有明确的宽高，默认最小2dp，否则太小看不清
                img.setMinimumWidth(DensityUtil.dp2px(context, 2));
                img.setMinimumHeight(DensityUtil.dp2px(context, 2));
            }
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            if (i == curSelectedIndex) {
                img.setImageDrawable(pointSelectedDrawable);
                lp.width = DensityUtil.dp2px(context, pointAttr.getPointMaxSize());
            } else {
                img.setImageDrawable(pointUnSelectedDrawable);

            }
            pointLayout.addView(img, lp);
        }
    }

    /*
    * 改变选中小圆点
    * */
    public void changeSelectedPoint(int index) {

        if (pointLayout.getChildCount() == 0) {
            return;
        }
        int pointCount = pointLayout.getChildCount();
        int position = index % pointCount;
        if (curSelectedIndex == position) {
            return;
        }

        ImageView curSelectedImg = (ImageView) pointLayout.getChildAt(position);
        ImageView lastSelectedImg = (ImageView) pointLayout.getChildAt(curSelectedIndex);
        addPointSelectedAnim(curSelectedImg, lastSelectedImg);
        curSelectedIndex = position;
    }

    /*
    * 添加小圆点选中动画
    * */
    private void addPointSelectedAnim(final ImageView selectedView, final ImageView lastSelectedView) {

        if (selectedView == null) {
            return;
        }
        selectedView.clearAnimation();
        selectedView.getLayoutParams().width = DensityUtil.dp2px(context, pointAttr.getPointSize());
        selectedView.requestLayout();
        selectedView.setImageDrawable(pointSelectedDrawable);

        ValueAnimator valueAnimator = ValueAnimator.ofInt(DensityUtil.dp2px(context, pointAttr.getPointSize()), DensityUtil.dp2px(context, pointAttr.getPointMaxSize()));
        valueAnimator.setDuration(250);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                Integer value = (Integer) animation.getAnimatedValue();
                selectedView.getLayoutParams().width = value;
                selectedView.requestLayout();

            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                addPointUnSelectedAnim(lastSelectedView);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
    }

    private void addPointUnSelectedAnim(final ImageView pointView) {

        if (pointView == null) {
            return;
        }
        pointView.clearAnimation();
        pointView.setImageDrawable(pointUnSelectedDrawable);

        ValueAnimator valueAnimator = ValueAnimator.ofInt(DensityUtil.dp2px(context, pointAttr.getPointMaxSize()), DensityUtil.dp2px(context, pointAttr.getPointSize()));
        valueAnimator.setDuration(250);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                Integer value = (Integer) animation.getAnimatedValue();
                pointView.getLayoutParams().width = value;
                pointView.requestLayout();
            }
        });
        valueAnimator.start();
    }

    public int getCurSelectedIndex() {
        return curSelectedIndex;
    }

    public void setCurSelectedIndex(int curSelectedIndex) {
        this.curSelectedIndex = curSelectedIndex;
    }

    public LinearLayout getPointLayout() {
        return pointLayout;
    }
}
